package cn.iocoder.yudao.module.pay.controller.app.wallet;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.wallet.AppPayWalletRespVO;
import cn.iocoder.yudao.module.pay.convert.wallet.PayWalletConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletTransactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @author jason
 */
@Tag(name = "用户 APP - 钱包")
@RestController
@RequestMapping("/pay/wallet")
@Validated
@Slf4j
public class AppPayWalletController {

    @Resource
    private PayWalletService payWalletService;

    @Resource
    private PayWalletTransactionService payWalletTransactionService;

    @GetMapping("/get")
    @Operation(summary = "获取钱包")
    @PreAuthenticated
    public CommonResult<AppPayWalletRespVO> getPayWallet(@RequestParam("userId") Long userId) {
        PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, UserTypeEnum.MEMBER.getValue());
        return success(PayWalletConvert.INSTANCE.convert(wallet));
    }


    @GetMapping("/bet/sum")
    @Operation(summary = "获得充值记录分页")
    public CommonResult<Map<String, Object>> betSum(@RequestParam("code") String code) {
        Map<String, Object> res = new HashMap<>();
        res.put("bet_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET.getType(), 0).get("sum_p"));

        return success(res);
    }


    @GetMapping("/bet/sum2")
    @Operation(summary = "投注流水")
    public CommonResult<Map<String, Object>> betSum2(@RequestParam("code") String code) {
        Map<String, Object> res = new HashMap<>();

        Object bet_sum_1_obj = payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET.getType(), 0).get("sum_p") ;
        Object bet_sum_2_obj = payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET.getType(), 1).get("sum_p") ;
        Object bet_sum_3_obj = payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET.getType(), 2).get("sum_p") ;

        BigDecimal b2 = new BigDecimal("-1");

        BigDecimal bet_sum_1 = ((BigDecimal) bet_sum_1_obj).multiply(b2) ;
        BigDecimal bet_sum_2 = ((BigDecimal) bet_sum_2_obj).multiply(b2) ;
        BigDecimal bet_sum_3 = ((BigDecimal) bet_sum_3_obj).multiply(b2) ;


        res.put("bet_sum_1", bet_sum_1);
        res.put("bet_sum_2", bet_sum_2);
        res.put("bet_sum_3", bet_sum_3);

        return success(res);
    }



    @GetMapping("/recharge/sum")
    @Operation(summary = "获得充值记录分页")
    public CommonResult<Map<String, Object>> rechargeSum(@RequestParam("code") String code) {
        Map<String, Object> res = new HashMap<>();
        res.put("recharge_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 1).get("sum_p"));
        res.put("f_recharge_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 1).get("sum_p"));
        res.put("s_recharge_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 2).get("sum_p"));
        res.put("t_recharge_sum_p", 0);
        res.put("recharge_count_p", payWalletTransactionService.rechargeSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 1).get("sum_p"));
        res.put("f_recharge_count_p", payWalletTransactionService.rechargeSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 1).get("sum_p"));
        res.put("s_recharge_count_p", payWalletTransactionService.rechargeSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 2).get("sum_p"));
        res.put("t_recharge_count_p", 0);
        return success(res);
    }

    @GetMapping("/commission/sum")
    @Operation(summary = "获得充值记录分页")
    public CommonResult<Map<String, Object>> commissionSum(@RequestParam("code") String code) {
        Map<String, Object> res = new HashMap<>();
        res.put("recharge_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 1).get("sum_p"));
        res.put("f_recharge_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 1).get("sum_p"));
        res.put("s_recharge_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.RECHARGE.getType(), 2).get("sum_p"));
        res.put("bet_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET_COMMISSION.getType(), 0).get("sum_p"));
        res.put("f_bet_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET_COMMISSION.getType(), 1).get("sum_p"));
        res.put("s_bet_sum_p", payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET_COMMISSION.getType(), 2).get("sum_p"));
        return success(res);
    }

    @GetMapping("/commission/list")
    @Operation(summary = "获得充值记录分页")
    public CommonResult<List<PayWalletTransactionDO>> commissionList(@RequestParam("code") String code) {
        return success(payWalletTransactionService.list(code, PayWalletBizTypeEnum.BET_COMMISSION.getType()));
    }

    @GetMapping("/bet/list")
    @Operation(summary = "每日投注流水")
    public CommonResult<List<Map<String, Object>>> betList(@RequestParam("code") String code,
                                                              @RequestParam(name="startTime",required = false) String startTimeStr,
                                                              @RequestParam(name="endTime",required = false) String endTimeStr,
                                                              @RequestParam(name="bizType",defaultValue = "8",required = false) Integer  bizType) {

        // 当前时间
        LocalDate date = LocalDate.now();
        LocalDate StartTime = StrUtil.isNotBlank(startTimeStr)? LocalDate.parse(startTimeStr) : date.minusDays(10) ;
        LocalDate EndTime = StrUtil.isNotBlank(endTimeStr)? LocalDate.parse(endTimeStr) :  date ;


        return success(payWalletTransactionService.list2(code,bizType,StartTime,EndTime));

        //return success(payWalletTransactionService.list(code, PayWalletBizTypeEnum.BET_COMMISSION.getType()));
    }

    @GetMapping("/bet/listSum")
    @Operation(summary = "每日投注流水")
    public CommonResult<Map<String, Object>> betListSum(@RequestParam("code") String code,
                                                           @RequestParam(name="startTime",required = false) String startTimeStr,
                                                           @RequestParam(name="endTime",required = false) String endTimeStr,
                                                           @RequestParam(name="bizType",defaultValue = "8",required = false) Integer  bizType) {

        // 当前时间
        LocalDate date = LocalDate.now();
        LocalDate StartTime = StrUtil.isNotBlank(startTimeStr)? LocalDate.parse(startTimeStr) : date.minusDays(10) ;
        LocalDate EndTime = StrUtil.isNotBlank(endTimeStr)? LocalDate.parse(endTimeStr) :  date ;


        return success(payWalletTransactionService.listSum(code,bizType,StartTime,EndTime));

    }


    @GetMapping("/referrer/list")
    @Operation(summary = "每日投注流水")
    public CommonResult<List<Map<String, Object>>> referrerList(@RequestParam("code") String code,
                                                           @RequestParam(name="startTime",required = false) String startTimeStr,
                                                           @RequestParam(name="endTime",required = false) String endTimeStr,
                                                           @RequestParam(name="bizType",defaultValue = "8",required = false) Integer  bizType) {

        // 当前时间
        LocalDate date = LocalDate.now();
        LocalDate StartTime = StrUtil.isNotBlank(startTimeStr)? LocalDate.parse(startTimeStr) : date.minusDays(10) ;
        LocalDate EndTime = StrUtil.isNotBlank(endTimeStr)? LocalDate.parse(endTimeStr) :  date ;


        return success(payWalletTransactionService.referrerList(code,StartTime,EndTime));


    }

    @GetMapping("/level/sum")
    @Operation(summary = "各级总金额")
    public CommonResult<Map<String, Object>> levelSum(@RequestParam("code") String code,
                                                    @RequestParam(name="level",defaultValue = "1",required = false) Integer level,
                                                      @RequestParam(name="bizType",defaultValue = "11",required = false) Integer  bizType
    ) {
        Map<String, Object> res = new HashMap<>();

        Object bet_sum_1_obj = payWalletTransactionService.betSum(code, bizType, level).get("sum_p") ;
        ///Object bet_sum_2_obj = payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET.getType(), 1).get("sum_p") ;
        //Object bet_sum_3_obj = payWalletTransactionService.betSum(code, PayWalletBizTypeEnum.BET.getType(), 2).get("sum_p") ;

        //BigDecimal b2 = new BigDecimal("-1");

        //BigDecimal bet_sum_1 = ((BigDecimal) bet_sum_1_obj).multiply(b2) ;
        //BigDecimal bet_sum_2 = ((BigDecimal) bet_sum_2_obj).multiply(b2) ;
        //BigDecimal bet_sum_3 = ((BigDecimal) bet_sum_3_obj).multiply(b2) ;


        res.put("level_sum", bet_sum_1_obj);
       // res.put("bet_sum_2", bet_sum_2);
       // res.put("bet_sum_3", bet_sum_3);

        return success(res);
    }



}
